// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Globalization;
using ValidationAspects.Exceptions;
using ValidationAspects.Sdk;

namespace ValidationAspects.Factories
{
	public class IsDate : ValidatorFactoryBase<string>
	{
		public string Format { get; set; }

		protected override Action<string, IValidationContext> ValidateAction
		{
			get
			{
				return (v, c) =>
				       {
				       	if (v != null && !string.IsNullOrEmpty(Format))
				       	{
				       		try
				       		{
				       			DateTime.ParseExact(v, "d", new DateTimeFormatInfo {ShortDatePattern = Format});
				       		}
				       		catch (FormatException)
				       		{
				       			throw new ValidateIsDateException(Format, v);
				       		}
				       	}
				       };
			}
		}
	}
}